/**
 * Created by wangshaohua on 2018/1/13.
 */
moduleOrganizationClient.controller("OrganizationClientDetail", function (EmsMgtAdminResource, LanguageService, $$CambioConfig, $scope, $log, $state, $stateParams, GeneralResource, LoadingService, ConstantService, DialogService) {
    initPage($scope);
    $scope.$on("$viewContentLoaded", function () {
        pageOnLoad();
        ConstantService.addState($state.current, true, LanguageService.language('企业账户详细'));
        $scope.organizationClientId = $stateParams.organizationClientId;
        $scope.organizationClientCode = $stateParams.organizationClientCode;
        $scope.reset();
        $scope.selectTab('tabAdmin');
    });

    $scope.paginationAdmin = new Pagination();
    $scope.paginationAdmin.setPageClickFn($scope.loadAdmin);

    $scope.paginationGuest = new Pagination();
    $scope.paginationGuest.setPageClickFn($scope.loadGuest);

    $scope.currentTabId = 0;
    $scope.selectTab = function (tabId) {
        if (tabId === 'tabAdmin') {
            $scope.loadAdmin(1);
            $scope.currentTabId = tabId;
        }
        if (tabId === 'tabGuest') {
            $scope.loadGuest(1);
            $scope.currentTabId = tabId;
        }
    };

    // ------------------------------------------------------------------------------------------------------
    $scope.reset = function () {
        $scope.queryPara = {
            className: Models.User.className,
            query: {
                roleCode: {
                    field: Models.User.query.roleCode
                },
                organizationClientId: {
                    field: Models.User.query.organizationClientId,
                    value: $scope.organizationClientId
                },
                active: {
                    field: 'active',
                    value: true
                }
            }
        };
    }


    $scope.loadAdmin = function (pageNo) {
        $scope.queryPara.query.roleCode.value = 'ROLE_COMPANY_SERVICE';
        GeneralResource.queryPage({pageNo: pageNo - 1, pageSize: 10}, $scope.queryPara, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.admins = queryResult.data;
            $scope.paginationAdmin.setPage(queryResult.data, pageNo);
        });
    };

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------

    $scope.loadGuest = function (pageNo) {
        $scope.queryPara.query.roleCode.value = 'ROLE_COMPANY_GUEST';
        GeneralResource.queryPage({pageNo: pageNo - 1, pageSize: 10}, $scope.queryPara, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.guests = queryResult.data;
            $scope.paginationGuest.setPage(queryResult.data, pageNo);
        });
    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------

    $scope.add = function (roleCode) {
        $scope.currentUser = {
            roleCode: roleCode,
            companyCode: $scope.organizationClientCode
        }
        DialogService.open("organizationClientUserMgt", $scope, function () {
            if ($scope.currentUser.roleCode === 'ROLE_COMPANY_SERVICE') {
                $scope.loadAdmin(1);
            }
            if ($scope.currentUser.roleCode === 'ROLE_COMPANY_GUEST') {
                $scope.loadGuest(1);
            }
        });
    }

    $scope.unActiveUser = function (record) {
        Alert.confirm(function () {
            EmsMgtAdminResource.unActive({userId: record.id}, {}, function (result) {
                $scope.selectTab('tabAdmin');
            });
        })
    }

    $scope.changePassword = function (record) {
        $scope.currentUser = record;
        DialogService.open("clientUserChangePassword", $scope);
    }
});